package com.bill.common.news.service;

import com.bill.common.news.dao.ICategoryDAO;
import com.bill.common.news.dao.ITopicDAO;
import com.bill.common.news.dto.CategoryDTO;
import com.bill.common.news.dto.IndexMenuDTO;
import com.bill.common.news.dto.TopicDTO;
import com.bill.common.news.entity.Category;
import com.bill.common.news.entity.Topic;
import com.sinba.common.dto.PageDTO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

public class CategoryService
    implements ICategoryService
{
    @Autowired
    private ICategoryDAO categoryDAO;
    @Autowired
    private ITopicDAO topicDAO;

    //分页查询分类
    @Transactional
    public PageDTO findCategoryPager( CategoryDTO dto, String sord, String sidx )
    {
        PageDTO p = categoryDAO.findCategoryPager( dto, sord, sidx );
        List<Category> categoryList = (List<Category>) p.getData(  );
        List<CategoryDTO> categoryDTOList = new ArrayList<CategoryDTO>(  );

        for ( Category entity : categoryList )
        {
            CategoryDTO categoryDTO = new CategoryDTO(  );
            categoryDTO.entity2dto( entity, categoryDTO );

            Topic topic = entity.getTopic(  );

            if ( topic != null )
            {
                categoryDTO.setTopicId( topic.getTopicId(  ) );
                categoryDTO.setTopicName( topic.getTopicName(  ) );
            }

            categoryDTOList.add( categoryDTO );
        }

        p.setData( categoryDTOList );

        return p;
    }

    //保存分类 
    @Transactional
    public void saveCategory( CategoryDTO dto )
    {
        Category entity = new Category(  );
        dto.dto2entity( dto, entity );

        if ( dto.getTopicId(  ) != null )
        {
            entity.setTopic( topicDAO.findById( dto.getTopicId(  ) ) );
        }

        categoryDAO.save( entity );
    }
    ;

    //编辑分类
    @Transactional
    public void editCategory( CategoryDTO dto )
    {
        Category entity = new Category(  );
        dto.dto2entity( dto, entity );

        if ( dto.getTopicId(  ) != null )
        {
            entity.setTopic( topicDAO.findById( dto.getTopicId(  ) ) );
        }

        categoryDAO.merge( entity );
    }
    ;

    //删除分类
    @Transactional
    public void deleteCategory( Long[] ids )
    {
        categoryDAO.deleteByIds( ids );
    }
    ;

    //根据ID查询分类
    @Transactional
    public CategoryDTO findCategoryInfo( Long id )
    {
        Category category = categoryDAO.findById( id );
        CategoryDTO dto = new CategoryDTO(  );
        dto.entity2dto( category, dto );

        if ( ( category != null ) && ( category.getTopic(  ) != null ) )
        {
            dto.setTopicId( category.getTopic(  ).getTopicId(  ) );
        }

        return dto;
    }
    ;

    //查询主页面菜�?
    @Transactional
    public List<IndexMenuDTO> findIndexMenu( TopicDTO topicQueryDTO, CategoryDTO categoryQueryDTO )
    {
        List<IndexMenuDTO> indexMenuDTOs = new ArrayList<IndexMenuDTO>(  );
        List<Topic> topics = topicDAO.findTopicAll( topicQueryDTO, "asc", "topicOrder" );

        if ( topics != null )
        {
            for ( Topic topic : topics )
            {
                IndexMenuDTO indexMenuDTO = new IndexMenuDTO(  );
                indexMenuDTO.setTopicName( topic.getTopicName(  ) );
                indexMenuDTO.setTopicId( topic.getTopicId(  ) );

                categoryQueryDTO.setTopicId( topic.getTopicId(  ) );

                List<Category> categorys = categoryDAO.findCategoryAll( categoryQueryDTO, "desc", "categoryOrder" );

                List<CategoryDTO> categoryDTOs = new ArrayList<CategoryDTO>(  );

                if ( categorys != null )
                {
                    for ( Category category : categorys )
                    {
                        CategoryDTO categoryDTO = new CategoryDTO(  );
                        categoryDTO.entity2dto( category, categoryDTO );
                        categoryDTOs.add( categoryDTO );
                    }
                }

                indexMenuDTOs.add( indexMenuDTO );

                indexMenuDTO.setCategoryDTO( categoryDTOs );
            }
        }

        return indexMenuDTOs;
    }
}
